import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

// https://vite.dev/config/
export default defineConfig({
  plugins: [vue(), vueDevTools()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    // 引入文件时，可以忽略以下的文件扩展名（后缀）
    extensions: ['.js', '.ts', '.vue', '.json', '.css']
  },
  build: {
    rollupOptions: {
      output: {
        entryFileNames: 'js/[name]-[hash:8].js',
        chunkFileNames: 'js/[name]-[hash:8].js',
        assetFileNames(chunkInfo) {
          const name = chunkInfo.names[0]
          console.log(name)

          if (name.endsWith('css')) {
            return 'css/[name]-[hash:8].css'
          }
          if (
            ['png', 'jpg', 'jpeg', 'svg', 'gif', 'ico'].some((item) =>
              name.endsWith(item)
            )
          ) {
            return 'assets/imgs/[name]-[hash:8].[ext]'
          }
          if (
            ['ttf', 'woff', 'woff2', 'eot'].some((item) => name.endsWith(item))
          ) {
            return 'assets/fonts/[name]-[hash:8].[ext]'
          }
          return 'assets/[name]-[hash:8].[ext]'
        }
      }
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 使用现代编译器
        api: 'modern-compiler',
        javascriptEnabled: true,
        // 引入全局scss变量
        additionalData: `@use "@/assets/styles/variables.scss" as *;`
      }
    }
  }
})
